<%@ page import="java.sql.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%!
    public  static boolean isEmpty(String s){
        return s==null||s.length()==0;
    }
%>
<%
    Object oname=session.getAttribute("name");
    if(oname==null){
        request.setAttribute("msg","请登录后查看");
        request.getRequestDispatcher("login.jsp").forward(request,response);
    }
    request.setCharacterEncoding("UTF-8");
    String id=request.getParameter("id");
    String sno=request.getParameter("sno");
    String name=request.getParameter("name");
    String sex=request.getParameter("sex");
    String phone=request.getParameter("phone");
    String username=request.getParameter("username");
    String password=request.getParameter("password");
    String clazz=request.getParameter("clazz");
    String homepath=request.getParameter("homepath");
    //1、注册驱动（公司报道）
    Class.forName("com.mysql.cj.jdbc.Driver");
    //2、获取连接（中标造桥）
    Connection connection = DriverManager.getConnection("jdbc:mysql://mysql.zhledu.cn:3306/school", "school", "zhli2008");
    //3、创建执行对象（获取货车）
    Statement stmt = connection.createStatement();
    //4、创建sql语句（下达命令）
    StringBuffer sb=new StringBuffer();
    sb.append("UPDATE t_user set sno='").append(sno).append("' ");
    sb.append(",name='").append(name).append("' ");
    sb.append(",sex='").append(sex).append("' ");
    sb.append(",phone='").append(phone).append("' ");
    sb.append(",username='").append(username).append("' ");
    sb.append(",password='").append(password).append("' ");
    sb.append(",clazz='").append(clazz).append("' ");
    sb.append(",home_path='").append(homepath).append("' ");
    sb.append(" where id=").append(id);
    String sql=sb.toString();
    try{
        if(sql.contains("where")) {
            int count = stmt.executeUpdate(sql);
            out.print("修改成功，2秒后跳转到首页");
            response.setHeader("refresh", "2;URL=index.jsp");
        }else{
            out.print("    <script>\n" +
                    "        alert(\"你是不是忘记加where条件了\")\n" +
                    "    </script>");
        }
    }catch (Exception e){
        request.setAttribute("name",name);
        request.setAttribute("sex",sex);
        request.setAttribute("phone",phone);
        request.setAttribute("username",username);
        request.setAttribute("password",password);
        request.setAttribute("clazz",clazz);
        request.setAttribute("homepath",homepath);
        request.setAttribute("msg",e.getLocalizedMessage());
        request.getRequestDispatcher("update.jsp").forward(request,response);
    }finally {
        connection.close();
    }
%>
